package sg.edu.nus.iss.vmcs.maintenance;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.awt.event.*;
import java.awt.*;

import sg.edu.nus.iss.vmcs.store.DrinksBrand;
import sg.edu.nus.iss.vmcs.store.DrinksStoreItem;
import sg.edu.nus.iss.vmcs.store.Store;
import sg.edu.nus.iss.vmcs.util.Undoable;

/**
 *
 *
 * @version 3.0 5/07/2003
 * @author Olivo Miotto, Pang Ping Li
 */

public class PriceDisplayListener implements Undoable {
	private MaintenanceController mctrl;
	
	//State Info
	private int curIdx;
	private int oldPrice;

	public PriceDisplayListener(MaintenanceController mc) {
		mctrl = mc;
	}
	public void actionPerformed(ActionEvent e) {
		TextField txt;

		String sp;
		int ip;

		txt = (TextField) e.getSource();
		sp = txt.getText();

		ip = Integer.parseInt(sp);
		
		//Store State Info for undo
		curIdx=mctrl.getDrinksDisplayCurIdx();
		DrinksStoreItem dsi=
			(DrinksStoreItem)mctrl.getMainController().getStoreController().getStoreItem(Store.DRINK, curIdx);
		oldPrice=((DrinksBrand)dsi.getContent()).getPrice();

		//Execute the command action
		mctrl.setPrice(ip);
		
		//Store the command
		mctrl.getCommandHistory().pushCommand((PriceDisplayListener)this.clone());		
	}
	
	public void undo(){
		mctrl.displayDrinks(curIdx);
		mctrl.setPrice(oldPrice);			
	}
	
	//Prototype Pattern
	public Object clone(){
		PriceDisplayListener o=new PriceDisplayListener(this.mctrl);
		o.curIdx=this.curIdx;
		o.oldPrice=this.oldPrice;
		return o;
	}
	
}